using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Net;

using Jeffrey.XFramework.XCrawl;
using Jeffrey.XFramework;

namespace Jeffrey.Xuthus
{
    public class XController
    {
        public XController(CrawlController cc)
        {
            m_Controller = cc;
            m_Errors     = new List<String>(LastNErrors);
            m_XMainForm  = new XMainForm(this, cc);
        }

        public Boolean Initialize()
        {
            if (!m_Controller.Initialize())
                return false;

            m_XMainForm.InitializeStuffs();

            return true;
        }
        public void Stop()
        {
            m_Controller.CrawlStop();
            m_Controller.Release();
        }

        public void RunSpdiers(String[] urls)
        {
            //set the config for this running...
            SetStuffsConfig(urls);
            m_Controller.CrawlStart();
        }

        public void SetStuffsConfig(String[] urls)
        {
            m_Controller.CrawlOrder.SourceLinks.Clear();
            if (urls != null)
            {
                foreach (String url in urls)
                    m_Controller.CrawlOrder.SourceLinks.Add(url);
            }

            //Set the Stuffs...
            m_Controller.CrawlOrder.BlockedRetry    = m_XMainForm.SettingsForm.DataSettings.BlockedRetry;
            m_Controller.CrawlOrder.DefaultEncoding = m_XMainForm.SettingsForm.DataSettings.DefaultEncoding;
            m_Controller.CrawlOrder.Depth           = m_XMainForm.SettingsForm.DataSettings.Depth;
            m_Controller.CrawlOrder.DnsCache        = m_XMainForm.SettingsForm.DataSettings.DnsCache;
            m_Controller.CrawlOrder.Interval        = m_XMainForm.SettingsForm.DataSettings.Interval;
            m_Controller.CrawlOrder.TimeOut         = m_XMainForm.SettingsForm.DataSettings.TimeOut * 1000;
            m_Controller.CrawlOrder.TotalPages      = m_XMainForm.SettingsForm.DataSettings.TotalPages * 10 * 1000;
            m_Controller.CrawlOrder.KeepAlive       = m_XMainForm.SettingsForm.DataSettings.KeepAlive;
            m_Controller.CrawlOrder.GC              = m_XMainForm.SettingsForm.DataSettings.GarbageCollection;
        }

        public Form MainForm
        {
            get { return m_XMainForm; }
        }

        public void ShowErrorDetails(Int32 ErrorNum)
        {
            m_XMainForm.ShowErrorDetails(m_Errors[ErrorNum % m_LastNErrors]);
        }

        public void LogErrors(Int32 ErrorNum, String error)
        {
            Int32 index = ErrorNum % m_LastNErrors;

            if(index >= m_Errors.Count)
            {
                for (Int32 i = m_Errors.Count; i <= index; i++)
                {
                    m_Errors.Add(String.Empty);
                }
            }

            m_Errors[index] = error;
        }

        public Int32 LastNErrors
        {
            get { return m_LastNErrors; }
        }

        public String InternetState
        {
            get 
            {
                String Internet = String.Empty;
                String LocalIP  = String.Empty;

                IPAddress[] ips = Dns.GetHostAddresses(String.Empty);
                foreach (IPAddress ip in ips)
                {
                    if (ip.ToString().Length < 0x10)
                        LocalIP = ip.ToString();
                }

                if (LocalIP == "127.0.0.1")
                    Internet = "Disconnected";
                else
                    CrawlToolKits.LANConnectedState(out Internet);

                Internet = "Internet: " + Internet;
                return Internet + "   " + "Machine IP: " + LocalIP + " ";
            }
        }

        public String GetSourceLinks()
        {
            StringBuilder urls = new StringBuilder(String.Empty);

            if (m_Controller.CrawlOrder.SourceLinks.Count == 0)
                return String.Empty;
            else
            {
                foreach (String str in m_Controller.CrawlOrder.SourceLinks)
                {
                    urls.Append(str + " ");
                }
                urls.Remove(urls.Length - 1, 1);
            }
            return urls.ToString();
        }

        public Boolean CreateFilter(ProcessorInfo filter)
        {
            return m_Controller.AddFilter(filter);
        }

        public Boolean AddProcessor(ProcessorInfo proinfo, ProcessorChain.ProcessorChainName name)
        {
            return m_Controller.AddProcessor(proinfo, name);
        }

        public void Paused()
        {
            m_Controller.SuspendAllSpiders();
        }

        public void Continue()
        {
            m_Controller.ContinueAllSpiders();
        }

        public Boolean LoadConfig()
        {
            return m_Controller.LoadConfig();
        }

        public Boolean SaveConfig()
        {
            return m_Controller.SaveConfig();
        }

        public void DeleteTreeItem(String procname, Boolean filter)
        {
            if (filter)
                m_Controller.DeleteFilterFromProcessor(procname);
            else
                m_Controller.DeleteProcessor(procname);
        }

        public void DisableItem(String name, Boolean disabled, Boolean filter)
        {
            if(filter)
                m_Controller.DisableFilter(name, disabled);
            else
                m_Controller.DisableProcessor(name, disabled);
        }

        private Int32 m_LastNErrors = 50;

        private List<String>    m_Errors;
        private CrawlController m_Controller;
        private XMainForm       m_XMainForm;
    }
}
